﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Input;
using TimedTask.Tools;
using TimedTask.Models;
using TimedTask.Core;
using TimedTask.BLL;
using System.Collections.ObjectModel;
using MSL.Tool;

//----------------------------------------------------------------*/
// 版权所有：
// 文 件 名：ConfigViewModel.cs
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2014/6/21 17:05:39
// 修改描述：
//----------------------------------------------------------------*/
namespace TimedTask.ViewModel
{
    public class ConfigViewModel : BaseViewModel
    {
        private ConfHelper config = null;
        private SysHelper _sys = new SysHelper();
        private CityBLL _cityBLL = new CityBLL();

        public ConfigViewModel()
        {
            if (!IsInDesignMode)// 不是在使用Blend设计的模式下
            {
                Loading();
            }
            this.RadioCommand = new BaseCommand((Object m) =>
            {
                int n = Int32.Parse(m.ToString());
                if (n == 1 || n == 2)
                    this.BtnOKText = "保存";
                else if (n == 3)
                    this.BtnOKText = "保存";
            });
            this.SaveCommand = new BaseCommand((Object parameter) => Save());
            this.SelectionMinuteChangedCommand = new BaseCommand((Object m) =>
            {
                this.LockMinute = (Int32)m;
            });
            this.TextCityChanged = new BaseCommand((Object parameter) =>
            {
                string name = parameter.ToString();
                if (name.Length == 0)
                {
                    return;
                }
                List<City> citys = _cityBLL.Query(name);
                if (citys != null && citys.Count > 0)
                {
                    CityPopOpen = true;
                    Citys.Clear();
                    foreach (var item in citys)
                    {
                        Citys.Add(new SelectListItem { Text = item.Name, Value = item.Code });
                    }
                }
            });
            this.TextLostFocusChanged = new BaseCommand((Object parameter) =>
            {
                CityPopOpen = false;
            });
            this.SelectCityChanged = new BaseCommand((Object parameter) =>
            {
                CityPopOpen = false;
                SelectListItem item = (SelectListItem)parameter;
                if (item != null)
                {
                    this.SelectCity = new City() { Name = item.Text, Code = item.Value };
                }
            });
            this.OpenAppImgCommand = new BaseCommand((Object type) => OpenImage(type.ToString()));
        }

        #region 属性

        private string _btnText = "保存";
        private string _bgImg = Const.AppBgImg;
        private City _selectCity = new City() { Name = "北京", Code = "101010100" };
        private string _lockImg = Const.LockBgImg;
        private bool _isAutoRun = false;
        private bool _minToTray = false;
        private bool _saveLog = false;
        private bool _showNews = true;//启动时是否显示资讯
        private bool _cityPopOpen = false;//城市下拉是否显示
        private String _shutDownContent;
        private bool _showRemind = false;//是否开启关机提醒事项
        private ObservableCollection<SelectListItem> _Citys = new ObservableCollection<SelectListItem>();

        private int _lockMinute = Const.LockMinute;
        private List<int> _lstMinute;

        /// <summary> 城市列表 </summary>
        public ObservableCollection<SelectListItem> Citys
        {
            get { return _Citys; }
            set
            {
                base.SetProperty<ObservableCollection<SelectListItem>>(ref this._Citys, value);
            }
        }
        /// <summary> 已选择城市 </summary>
        public City SelectCity
        {
            get { return _selectCity; }
            set
            {
                base.SetProperty<City>(ref this._selectCity, value);
            }
        }

        /// <summary> 城市下拉是否显示 </summary>
        public bool CityPopOpen
        {
            get { return _cityPopOpen; }
            set
            {
                base.SetProperty<bool>(ref this._cityPopOpen, value);
            }
        }
        /// <summary> 是否开启关机提醒事项 </summary>
        public bool ShowShutDownRemind
        {
            get { return _showRemind; }
            set
            {
                base.SetProperty<bool>(ref this._showRemind, value);
            }
        }
        /// <summary> 关机提醒内容 </summary>
        public string ShutDownContent
        {
            get { return _shutDownContent; }
            set
            {
                base.SetProperty<string>(ref this._shutDownContent, value);
            }
        }

        /// <summary> 关闭最小化 </summary>
        public bool ShowNews
        {
            get { return _showNews; }
            set
            {
                base.SetProperty<bool>(ref this._showNews, value);
            }
        }
        /// <summary> 锁屏时间 </summary>
        public List<int> LstMinute
        {
            get { return _lstMinute; }
            set
            {
                base.SetProperty<List<int>>(ref this._lstMinute, value);
            }
        }
        /// <summary> 关闭最小化 </summary>
        public bool MinToTray
        {
            get { return _minToTray; }
            set
            {
                base.SetProperty<bool>(ref this._minToTray, value);
            }
        }
        /// <summary> 锁屏时间 分钟 </summary>
        public int LockMinute
        {
            get { return _lockMinute; }
            set
            {
                base.SetProperty<int>(ref this._lockMinute, value);
            }
        }
        /// <summary> 是否保存运行日志 </summary>
        public bool SaveLog
        {
            get { return _saveLog; }
            set
            {
                base.SetProperty<bool>(ref this._saveLog, value);
            }
        }
        /// <summary> 是否自启 </summary>
        public bool IsAutoRun
        {
            get { return _isAutoRun; }
            set
            {
                base.SetProperty<bool>(ref this._isAutoRun, value);
            }
        }
        /// <summary> 保存文字 </summary>
        public string BtnOKText
        {
            get { return _btnText; }
            set
            {
                base.SetProperty<string>(ref this._btnText, value);
            }
        }
        /// <summary> 窗体背景 </summary>
        public string BgImg
        {
            get { return _bgImg; }
            set
            {
                base.SetProperty<string>(ref this._bgImg, value);
            }
        }
        /// <summary> 锁屏背景 </summary>
        public string LockImg
        {
            get { return _lockImg; }
            set
            {
                base.SetProperty<string>(ref this._lockImg, value);
            }
        }

        #endregion

        #region 命令

        public ICommand RadioCommand { set; get; }
        /// <summary> 保存  </summary>
        public ICommand SaveCommand { get; set; }
        /// <summary> 城市框文本变化 </summary>
        public ICommand TextCityChanged { get; set; }
        /// <summary> 城市框文本失去焦点 </summary>  
        public ICommand TextLostFocusChanged { get; set; }
        /// <summary>
        /// 选择城市
        /// </summary>
        public ICommand SelectCityChanged { get; set; }
        /// <summary> 选择图片  </summary>
        public ICommand OpenAppImgCommand { set; get; }
        /// <summary> 锁屏  </summary>
        public ICommand SelectionMinuteChangedCommand { set; get; }
        #endregion

        #region 方法

        private void OpenImage(string type)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Filter = "JPG文件|*.jpg|PNG文件|*.png";//全部|*.*||批处理|*.bat";
            if (ofd.ShowDialog() == true)
            {
                string organPath = ofd.FileName;
                string fName = Helper.GetQunueNo() + ofd.FileName.Substring(ofd.FileName.LastIndexOf('.'));
                string newPath = Const.StartPath + "\\Bg\\" + fName;
                MSL.Tool.IOHelper.CopyFile(organPath, newPath);
                if (type == "1")//窗体
                {
                    this.BgImg = "\\Bg\\" + fName;
                    Const.AppBgImg = newPath;
                }
                else
                {
                    this.LockImg = "\\Bg\\" + fName;
                    Const.LockBgImg = newPath;
                }
            }
        }
        private void Save()
        {
            if (this.BtnOKText == "确定")
            {
                //this.Close();
                return;
            }
            this._sys.AutoStartup(this.IsAutoRun);
            Const.MinToTray = this.MinToTray;

            this.config.SetValue("LockMinute", this.LockMinute.ToString());
            this.config.SetValue("SaveLog", this.SaveLog ? "1" : "0");
            this.config.SetValue("ShowShutDownRemind", this.ShowShutDownRemind ? "1" : "0");
            this.config.SetValue("ShutDownContent", this.ShutDownContent);
            try
            {
                if (this.SelectCity != null)
                {
                    this.config.SetValue("CityCode", this.SelectCity.Name + "|" + this.SelectCity.Code);
                }
                this.config.SetValue("AppBgImg", this.BgImg);
                this.config.SetValue("LockBgImg", this.LockImg);
                this.config.SetValue("MinToTray", Const.MinToTray ? "1" : "0");
                this.config.Save();

                Helper.AlertSuccess("保存成功，重启后生效！");
            }
            catch (Exception ex)
            {
                LogHelper.SaveErrorLog("Config btnOK_Click", ex);
                Helper.AlertError("保存失败！");
            }
        }
        private void Loading()
        {
            this.config = new ConfHelper(Const.Config);
            this.BgImg = this.config.GetValue("AppBgImg");
            this.LockImg = this.config.GetValue("LockBgImg");

            if (!String.IsNullOrEmpty(Const.CityName))
            {
                SelectCity = new City() { Name = Const.CityName, Code = Const.CityCode };
            }

            //自动启动
            if (this._sys.IsAutoStartup())
                this.IsAutoRun = true;

            this.LstMinute = new List<int>();
            for (int i = 1; i < 20; i++)
            {
                this.LstMinute.Add(i);
            }

            this.MinToTray = Const.MinToTray;
            this.SaveLog = Const.SaveLog;
            //this.ShowNews = Const.ShowNews;
            this.ShowShutDownRemind = Const.ShowShutDownRemind;
            this.ShutDownContent = Const.ShutDownContent;
        }
        #endregion
    }
}
